unit ucTimeFuncs;

interface

    function DateTimeToSeconds(newDate:TDateTime):int64;
    function SecondsToDateTime(value:int64):string;
    function DateTimeStrToSeconds(value:string):int64;
    function TimeStrToSeconds(value:string):int64;

implementation

uses Math, SysUtils, DateUtils,
    ucFuncs, ucSplitLine;

function DateTimeToSeconds(newDate:TDateTime):int64; // this will now not fail if it crosses a month! hopefully!
const
  DaysInYear: array[1..12] of Integer = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var
  myHour, myMin, mySec, myMilli,
  myYear, myMonth, myDay : word;

  nHour, nMin, nSec, nDay : int64;
  I : integer;
begin
  DecodeDateTime(newDate, myYear, myMonth, myDay,
                   myHour, myMin, mySec, myMilli);

  for I:= 1 to myMonth do
  begin
    if (IsLeapYear(myYear))and(I = 2) then
      myDay := myDay + (DaysInYear[I]+1) // Add month days into total time, with leap
    else
      myDay := myDay + DaysInYear[I]; // Add month days into total time
  end;

  nDay   := myDay*60*60*24;
  nHour  := myHour*60*60;
  nMin   := myMin*60;
  nSec   := mySec;

  result := (nDay+nHour+nMin+nSec);
end;

function SecondsToDateTime(value:int64):string;
var
  Days, Hours, Minutes, Seconds : integer;
begin
  Days    := (floor(value) div 86400);
  value := value - (Days*86400);

  Hours    := (floor(value) div 3600);
  value := value - (Hours*3600);

  Minutes  := (floor(value) div 60);
  value := value - (Minutes*60);

  Seconds := value;

  result := '';

  if (Days > 0) then
    result := result + AddLeadingZeroes(Days,2,'0',TRUE)+'d '+
              AddLeadingZeroes(Hours,2,'0',TRUE)+'h '+
              AddLeadingZeroes(Minutes,2,'0',TRUE)+'m '+
              AddLeadingZeroes(Seconds,2,'0',TRUE)+'s'
  else if (Hours > 0) then
    result := AddLeadingZeroes(Hours,2,'0',TRUE)+'h '+
              AddLeadingZeroes(Minutes,2,'0',TRUE)+'m '+
              AddLeadingZeroes(Seconds,2,'0',TRUE)+'s'
  else
    result := AddLeadingZeroes(Minutes,2,'0',TRUE)+'m '+
              AddLeadingZeroes(Seconds,2,'0',TRUE)+'s';
end;

function DateTimeStrToSeconds(value:string):int64;
var
  Date,Time :string;
  firstTime, firstDate : TDateTime;
  SplitLine : TSplitLine;
begin
//===== DateTime
  SplitLine := TSplitLine.Create;
  SplitLine.SetChar(' ','^','"');
  SplitLine.Explode( Value );

  Date := SplitLine.To_Str(0);
  Time := SplitLine.To_Str(1);

  firstTime := StrToTime(Time);
  firstDate := StrToDate(Date);

  result := DateTimeToSeconds( firstTime + firstDate );
end;

function TimeStrToSeconds(value:string):int64;
var
  Hours, Minutes, Seconds : int64;
  SplitLine : TSplitLine;
begin
  SplitLine := TSplitLine.Create;

  SplitLine.SetChar(':','^','"');

  SplitLine.Explode( Value );

  Hours   := SplitLine.To_Int(0)*60*60;
  Minutes := SplitLine.To_Int(1)*60;
  Seconds := SplitLine.To_Int(2);

  result := Hours + Minutes + Seconds;
end;




end.
